//
//  JSLBillEditController.m
//  JingleSpeedLoan
//
//  Created by 宋志彬 on 2018/8/5.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JSLBillEditController.h"
#import "JSLTableBillEditCell.h"
#import "ActionSheetPicker.h"
#import "NSDate+Addition.h"

@interface JSLBillEditController ()<UITableViewDelegate,UITableViewDataSource,JSLTableBillEditCellDelegate>
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic , strong) UITextField *textFieldSelected;
@property (nonatomic , strong) NSMutableDictionary *paramters;
@end

@implementation JSLBillEditController
- (NSMutableDictionary *)paramters {
    if (_paramters == nil) {
        _paramters = [[NSMutableDictionary alloc] init];
    }
    return _paramters;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = self.info[@"name"];
    
    self.paramters[@"name"] = self.info[@"name"];
    [self baseConfig:PageGobackTypePop];
    
    self.arrDataSource = [NSMutableArray arrayWithArray:@[
                                                          @[
                                                              [NSMutableDictionary dictionaryWithDictionary: @{ @"name":@"账单名称",
                                                                                                                @"contet" : self.info[@"name"],
                                                                                                                @"place" :@"请输入（如：买菜）",
                                                                                                                @"type":@"text-se"
                                                                                                                }]
                                                              ],
                                                          @[
                                                              [NSMutableDictionary dictionaryWithDictionary:@{ @"name":@"账单总金额(元)",
                                                                                                               @"contet" : @"",
                                                                                                               @"place" :@"请输入(如：100)",
                                                                                                               @"type":@"text"
                                                                                                               }],
                                                              [NSMutableDictionary dictionaryWithDictionary:@{ @"name":@"账单利率(%)",
                                                                                                               @"contet" : @"",
                                                                                                               @"place" :@"请输入(如：0.02)",
                                                                                                               @"type":@"text"
                                                                                                               }],
                                                              [NSMutableDictionary dictionaryWithDictionary: @{ @"name":@"账单周期(日)",
                                                                                                                @"contet" : @"",
                                                                                                                @"place" :@"请输入(如：30",
                                                                                                                @"type":@"text"
                                                                                                                }],
                                                              [NSMutableDictionary dictionaryWithDictionary: @{ @"name":@"账单还款时间",
                                                                                                                @"contet" : @"",
                                                                                                                @"place" :@"请选择还款时间",
                                                                                                                @"type":@"select"
                                                                                                                }]
                                                             
                                                             
                                                              ]
                                                          ]];
    
    [self tableView];
    
    
}

- (void)selectCycleDate:(NSDictionary *)dataInfo indexPath:(NSIndexPath *)indexPath{

    
    [ActionSheetDatePicker showPickerWithTitle:@"标题"
                                datePickerMode:UIDatePickerModeDate
                                  selectedDate:[NSDate date]
                                   minimumDate:nil
                                   maximumDate:nil
                                     doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
                                         
                                         NSLog(@"%@",selectedDate);
                                        
                                         
                                         NSInteger timeSp = [[NSNumber numberWithDouble:[selectedDate timeIntervalSince1970]] integerValue];
                                         // 将时间戳转化为对应格式时间
                                         NSString *selectItem = [NSDate timeStamp:timeSp*1000 timeFormat:@"yyyy-MM-dd"];

                                         if ([dataInfo[@"name"] isEqualToString:@"账单还款时间"]) {
                                             self.paramters[@"repaymentTime"] = selectItem;
                                         }
                                         
                                         [self.arrDataSource[indexPath.section][indexPath.row] setObject:selectItem forKey: @"contet"];
                                         [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
                                         
                                     } cancelBlock:^(ActionSheetDatePicker *picker) {
                                         NSLog(@"cancel");
                                         
                                     } origin:self.view];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - SYJRecordWarnCellDelegate
-(void)inputCellValueChanged:(NSString *)strValue withIndex:(NSIndexPath *)index{
    NSLog(@"%ld____%ld",index.section,index.row);
    
    
    if (index.section == 1) {
        if (index.row == 0) {
            self.paramters[@"money"] = strValue;
        }else if (index.row == 1) {
            self.paramters[@"moneylixi"] = strValue;
        }else if (index.row == 2) {
            self.paramters[@"loanTime"] = strValue;
        }
    }
    
    
    
    
}

- (void)textFiledBegin:(UITextField *)textField{
    
    self.textFieldSelected = textField ;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.arrDataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.arrDataSource[section] count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JSLTableBillEditCell *cell = [JSLTableBillEditCell cellWithTableView:tableView];
    [cell configInfo:self.arrDataSource[indexPath.section][indexPath.row] indexPath:indexPath];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *info = self.arrDataSource[indexPath.section][indexPath.row];
    
    if ([info[@"type"] isEqualToString:@"select"]) {
        [self selectCycleDate:info indexPath:indexPath];
    }else {
        
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10*AutoSizeScaleYY) backgroundColor:BACKGROUND_COLOR superView:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10 *AutoSizeScaleYY;
}

#pragma mark - get
- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-64 -49 ) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.scrollEnabled = YES;
        _tableView.rowHeight = 50 *AutoSizeScaleYY;
        _tableView.backgroundColor = BACKGROUND_COLOR;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
        

        
        //        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
        
        _tableView.tableHeaderView = [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10*AutoSizeScaleYY) backgroundColor:BACKGROUND_COLOR superView:nil];
        
        
        UIView *footerView = [JSLKitFactory jsl_viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 94*AutoSizeScaleYY) backgroundColor:BACKGROUND_COLOR superView:nil];
        UIButton *submitButton = [JSLKitFactory jsl_titleButtonWithFrame:CGRectMake(10*AutoSizeScaleXX, 50*AutoSizeScaleYY, SCREEN_WIDTH-20*AutoSizeScaleXX, 44*AutoSizeScaleYY) superView:footerView font:LIGHT_FONT(16) titleColor:[UIColor whiteColor] backgroundColor:[@"#4ea6fa" hexStringToColor] title:@"提交" target:self action:@selector(submitAction:)];
        
        submitButton.layer.mask = [JSLKitFactory roundedRect:submitButton.bounds cornerRadius:2];

        _tableView.tableFooterView =footerView;
        
    }
    
    return _tableView;
}

- (void)submitAction:(UIButton *)sender {
     if ([UserManager checkLogin:self]) {
         if ([self judgeNull]) {
             NSMutableArray *arr  =  [CacheManager getObject:@"LOAN_BILL_DATST"];
             
             NSMutableArray *arrc = [NSMutableArray arrayWithArray:arr];
             [arrc addObject:self.paramters];
             [CacheManager setObject:arrc forKey:@"LOAN_BILL_DATST"];
         }
         
         [self popToRootViewController];
     }
   
}

- (BOOL)judgeNull{
    if ([JSLKitFactory isNull:self.paramters[@"money"]]) {
        [self showMessage:@"请输入账单金额"];
        return NO;
        
    }else if ([JSLKitFactory isNull:self.paramters[@"moneylixi"]]) {
        [self showMessage:@"请输入账单日息"];
         return NO;
    }else if ([JSLKitFactory isNull:self.paramters[@"loanTime"]]) {
        [self showMessage:@"请选择账单日期"];
         return NO;
    }else if ([JSLKitFactory isNull:self.paramters[@"repaymentTime"]]) {
        [self showMessage:@"请输入账单还款日期"];
         return NO;
    }
    
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
